//
//  PhotoBrowserController.swift
//  LZFPhotoBrowser
//
//  Created by 梁梓烽 on 16/7/26.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

import UIKit

class PhotoBrowserController: UIViewController {

    //接受homeviewcontroller传递换来的shop模型数组
    var shops : [Shop]?
    
    var indexPath : NSIndexPath?
    
    

     private lazy var collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout:PhotoBrowserLayout() )
    
     private lazy var closeBtn = UIButton(title: "关闭", color: UIColor.whiteColor(), fontSize: 14,backColor: UIColor.grayColor())
    
     private lazy var saveBtn = UIButton(title: "保存", color: UIColor.whiteColor(), fontSize:14, backColor:UIColor.grayColor())
    
  
    
    //loadView
    override func  loadView() {
        super.loadView()
        //这里用bounds回不好使, 用frame
//        self.view.bounds.size.width+=10
          self.view.frame.size.width+=15
    }
    
    //viewDidload
    override func viewDidLoad() {
        super.viewDidLoad()
    
        //设置UI
        setUpUI()
        
        //刚开始的时候滚到indexPath所指的位置
        collectionView.scrollToItemAtIndexPath(indexPath!, atScrollPosition:.Left, animated: false)
         }
    }



/*
1.点击cell,自定义转场动画 显示控制器PhotoBrowserController
2.collectionView显示图片, cell 之间的间距
3.关闭按钮, 点击view自定义动画退出控制器
4.保存按钮 ,将图片保存到相册
*/


//reuseId
let ID =  "cell"
// MARK: - 设置UI
extension PhotoBrowserController {
    
   
     func setUpUI(){
        
        //添加collectionView控件
        view.addSubview(collectionView)
        collectionView.frame  =  view.bounds
    
         collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.registerClass(PhotoBrowserCell.self, forCellWithReuseIdentifier: ID)
        
        
        
        //添加按钮
        view.addSubview(closeBtn)
        view.addSubview(saveBtn)
        
        let  btnX : CGFloat = 20
        let  btnWidth : CGFloat = 90
        let  btnHeight  : CGFloat = 34
        let  btnY : CGFloat = UIScreen.mainScreen().bounds.height-btnHeight-20
        
    closeBtn.frame =  CGRect(x: btnX, y: btnY, width: btnWidth, height: btnHeight)
    saveBtn.frame = CGRect(x:( UIScreen.mainScreen().bounds.width-btnWidth-btnX), y: btnY, width: btnWidth, height: btnHeight)
    
        //默认私有方法是不加到类的方法列表中,注意私有方法要用@objc修饰才会加到类的方法列表中,这样你才可以发消息去找到它
       closeBtn.addTarget(self, action: "closeBtnClick", forControlEvents: .TouchUpInside)
       saveBtn.addTarget(self, action: "saveBtnClick", forControlEvents: .TouchUpInside)
    }
}

// MARK: - 按钮响应方法
extension PhotoBrowserController{

  @objc private func closeBtnClick() {
       dismissViewControllerAnimated(true, completion: nil)
    }
    
  @objc  private func saveBtnClick() {
        //拿到照片,保存到相册
    // 1.取出正在屏幕中显示的Cell
    let cell = collectionView.visibleCells().first as! PhotoBrowserCell
    
    // 2.取出image
    let image = cell.imageView.image
    
    // 3.写入相册
    UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)}
    

    
}

// MARK: - uicolliceionview数据源代理方法

extension PhotoBrowserController : UICollectionViewDataSource,UICollectionViewDelegate
{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
       
       return self.shops!.count
    }
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        
       let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ID, forIndexPath: indexPath) as! PhotoBrowserCell
        
       cell.shop = self.shops![indexPath.item]
        
       return cell
        
    }
    
     func  collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        
      closeBtnClick()
    
    }
    
   
    

    
}

// MARK: -DismissDeleagte
extension PhotoBrowserController:DismissDeleagte{
    
    func currentIndexPath()->NSIndexPath{
        
       let cell = collectionView.visibleCells().first! as! PhotoBrowserCell
       return collectionView.indexPathForCell(cell)!
    }
    
    
    func imageView(indexPath : NSIndexPath)->UIImageView{
        
        let imageView = UIImageView()
        
       let cell = collectionView.cellForItemAtIndexPath(indexPath) as! PhotoBrowserCell
        
        imageView.image = cell.imageView.image
        
        return imageView
    }
    

}


